//
//  MusicViewController.m
//  Project_ZFR_MUSIC
//
//  Created by 张福润 on 16/2/24.
//  Copyright © 2016年 张福润. All rights reserved.
//

#import "MusicViewController.h"

#import "MusicTableViewCell.h"
#import "MusicModel.h"
#import "RequestUrl.h"
#import "imageDownloadOperation.h"
#import "MusicPlayerViewController.h"
#define CELLID @"music_cell"
@interface MusicViewController ()<UITableViewDataSource,UITableViewDelegate,imageDownloadOperationDelegate>

//队列
@property (nonatomic,strong)NSOperationQueue *queue;

//pic的dic
@property (nonatomic,strong)NSMutableDictionary *allUrlDic;

//保存正在下载的图片的队列字典
@property (nonatomic,strong)NSMutableDictionary *operationDownLoadingDic;


@end

@implementation MusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setViews];
    [self setDelegate];
    [self setData];
    // Do any additional setup after loading the view.
}
#pragma mark - viewdidload的方法封装
-(void)setViews{
    self.tableView=[[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"MusicTableViewCell" bundle:nil] forCellReuseIdentifier:CELLID];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    UIImageView *imV=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"desktop.jpg"]];
    [self.tableView setBackgroundView:imV];
    
    
}

-(void)setDelegate{
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
}

-(void)setData{
    __weak typeof(self) weakSelf=self;
    RequestUrl *request=[[RequestUrl alloc]init];
    [request requestMusicWithSeccess:^(id item) {
        weakSelf.arrayAllData=item;
        [weakSelf.tableView reloadData];
    } faile:^(NSError *err) {
        NSLog(@"%@",err);
    }];
}

#pragma mark - tableView的cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MusicTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CELLID forIndexPath:indexPath];
    MusicModel *model=self.arrayAllData[indexPath.row];
    cell.labelName.text=model.name;
    cell.labelSinger.text=model.singer;
    cell.backgroundColor=[UIColor clearColor];
    //初始化线程块
    
    if([self.allUrlDic.allKeys containsObject:model.picUrl]){
        cell.imV.image=self.allUrlDic[model.picUrl];
    }
    else{
        cell.imV.image=[UIImage imageNamed:@"1"];
        //如果正在拖拽或者将要减速的时候,不处理图片,只有tableView停止的时候才会处理图片
        if(self.tableView.dragging||self.tableView.decelerating){
            return cell;
        }
        //如果图片在operation字典中,不再添加队列
        if([self.operationDownLoadingDic.allKeys containsObject:model.picUrl]){
            
        }

        else{
            imageDownloadOperation *imageDownload=[[imageDownloadOperation alloc]init];
            imageDownload.picUrl=model.picUrl;
            imageDownload.index=indexPath;
            imageDownload.delegate=self;
            [self.queue addOperation:imageDownload];
            [self.operationDownLoadingDic setValue:imageDownload forKey:model.picUrl];
        }
        
    }
    
    return cell;
}



#pragma mark - 代理,图片下载完执行
-(void)getImage:(UIImage *)image imageDownloadOperation:(imageDownloadOperation *)opetarion{
    MusicTableViewCell *cell=[self.tableView cellForRowAtIndexPath:opetarion.index];
    cell.imV.image=image;
    
    [self.allUrlDic setValue:image forKey:opetarion.picUrl];
    
    [self.operationDownLoadingDic removeObjectForKey:opetarion.picUrl];
}

-(void)getImageBlurPicImage:(UIImage *)blurPicImage imageDownloadOperation:(imageDownloadOperation *)opetarion{
    
}   
#pragma mark - 点击cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MusicPlayerViewController *player=[[MusicPlayerViewController alloc]init];
    
    MusicModel *model=self.arrayAllData[indexPath.row];
    player.musicModel=model;
    MusicTableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
    player.image=cell.imV.image;
    player.arrayAll=self.arrayAllData;
    player.number=indexPath.row;
    
    [self.navigationController pushViewController:player animated:YES];
}
#pragma mark - tableView的代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrayAllData.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
//减速到结束之前
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if(!decelerate){
        [self loadImage];
    }
}
//结束之后
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self loadImage];
}
-(void)loadImage{
    
    NSArray *arr=[self.tableView indexPathsForVisibleRows];
    [self.tableView reloadRowsAtIndexPaths:arr withRowAnimation:(UITableViewRowAnimationNone)];

}

//
#pragma mark - 懒加载
-(NSMutableArray *)arrayAllData{
    if(!_arrayAllData){
        _arrayAllData=[NSMutableArray array];
    }
    return _arrayAllData;
}
-(NSOperationQueue *)queue{
    if(!_queue){
        _queue=[[NSOperationQueue alloc]init];
        _queue.maxConcurrentOperationCount=3;
    }
    return _queue;
}
-(NSMutableDictionary *)allUrlDic {
    if(!_allUrlDic){
        _allUrlDic=[NSMutableDictionary dictionary];
    }
    return _allUrlDic;
}
-(NSMutableDictionary *)operationDownLoadingDic {
    if(!_operationDownLoadingDic){
        _operationDownLoadingDic=[NSMutableDictionary dictionary];
    }
    return _operationDownLoadingDic;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
